﻿using System.Drawing;
using System.Windows.Forms;

namespace Marmat.Forms
{

    /// <summary>
    /// 目录树优化处理
    /// </summary>
    /// <remarks></remarks>
    public static class TreeViewOptimization
    {
        private static System.Windows.Forms.TreeNode SNode;
        public static void Bind(System.Windows.Forms.TreeView TreeView)
        {
            TreeView.HotTracking = true;
            TreeView.AfterSelect += AfterSelect;
            TreeView.NodeMouseClick += NodeMouseClick;
        }

        private static void AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            e.Node.SelectedImageIndex = e.Node.ImageIndex;
            e.Node.ForeColor = Color.Blue;
            if (SNode is object)
            {
                SNode.ForeColor = Color.Black;
            }
            SNode = e.Node;
        }

        private static void NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (sender is System.Windows.Forms.TreeView treeView)
                treeView.SelectedNode = e.Node;
        }


        /// <summary>
        /// 向上移动目录树节点。
        /// </summary>
        /// <param name="TreeNode"></param>
        /// <remarks></remarks>
        public static void MoveUp(System.Windows.Forms.TreeNode TreeNode)
        {
            if (TreeNode is object)
            {
                if (TreeNode.Index > 0)
                {
                    var TreeView = TreeNode.TreeView;
                    var ond = TreeNode;
                    TreeView.SelectedNode = TreeView.SelectedNode.PrevNode;
                    if (ond.Level == 0)
                    {
                        TreeView.Nodes.Insert(ond.PrevNode.Index, (System.Windows.Forms.TreeNode)ond.Clone());
                    }
                    else
                    {
                        ond.Parent.Nodes.Insert(ond.PrevNode.Index, (System.Windows.Forms.TreeNode)ond.Clone());
                    }

                    ond.Remove();
                    TreeView.SelectedNode = TreeView.SelectedNode.PrevNode;
                    TreeView.ExpandAll();
                }
            }
        }

        /// <summary>
        /// 向下移动目录树节点。
        /// </summary>
        /// <param name="TreeNode"></param>
        /// <remarks></remarks>
        public static void MoveDown(System.Windows.Forms.TreeNode TreeNode)
        {
            if (TreeNode is object)
            {
                if (TreeNode.NextNode is object)
                {
                    var TreeView = TreeNode.TreeView;
                    var ond = TreeView.SelectedNode;
                    TreeView.SelectedNode = TreeView.SelectedNode.NextNode;
                    if (ond.Level == 0)
                    {
                        TreeView.Nodes.Insert(ond.NextNode.Index + 1, (System.Windows.Forms.TreeNode)ond.Clone());
                    }
                    else
                    {
                        ond.Parent.Nodes.Insert(ond.NextNode.Index + 1, (System.Windows.Forms.TreeNode)ond.Clone());
                    }

                    ond.Remove();
                    TreeView.SelectedNode = TreeView.SelectedNode.NextNode;
                    TreeView.ExpandAll();
                }
            }
        }

        /// <summary>
        /// 向左移动目录树节点。
        /// </summary>
        /// <param name="TreeNode"></param>
        /// <remarks></remarks>
        public static void MoveLeft(System.Windows.Forms.TreeNode TreeNode)
        {
            if (TreeNode.TreeView.SelectedNode is object)
            {
                var node = TreeNode;
                if (TreeNode.Level > 0)
                {
                    System.Windows.Forms.TreeNodeCollection nodes;
                    int index = node.Parent.Index + 1;
                    if (node.Parent.Level == 0)
                    {
                        nodes = node.TreeView.Nodes;
                    }
                    else
                    {
                        nodes = node.Parent.Parent.Nodes;
                    }

                    node.Parent.Nodes.Remove(node);
                    nodes.Insert(index, node);
                    node.TreeView.SelectedNode = node;
                }
            }
        }

        /// <summary>
        /// 向右移动目录树节点。
        /// </summary>
        /// <param name="TreeNode"></param>
        /// <remarks></remarks>
        public static void MoveRight(System.Windows.Forms.TreeNode TreeNode)
        {
            if (TreeNode is object)
            {
                var node = TreeNode;
                if (node.Index != 0)
                {
                    var nodes = node.PrevNode.Nodes;
                    if (node.Parent is object)
                    {
                        node.Parent.Nodes.Remove(node);
                    }
                    else
                    {
                        node.TreeView.Nodes.Remove(node);
                    }

                    nodes.Add(node);
                    node.TreeView.SelectedNode = node;
                }
            }
        }
    }
}